<?php

class UserGenerator
{
	private $user="root";
	private $password="";
	private $database="staty";
	private $host="localhost";
	private $dtStartDate;
	private $aTerytory;
	private $iDays;
	private $iDot='';
	
	function __destruct()
	{
       mysql_close();
    }
	
	function __construct()
	{      	
		$this->user='root';
		$this->password='';
		$this->database='ogrod3d';
		$this->host='localhost';
		$this->dtStartDate=new DateTime('06/30/2011');
		$this->Terytory=array();
		$this->iDays=$this->GetDaysBetweenDates("01/01/1972","01/01/1991");
		$this->iRegisterDays=$this->GetDaysBetweenDates("11/01/2010","08/15/2011");
		mysql_connect($this->host,$this->user,$this->password);
		mysql_select_db($this->database);
		mysql_query('SET character_set_connection=utf8');
		mysql_query('SET character_set_client=utf8');
		mysql_query('SET character_set_results=utf8'); 		      	    	
    }
	 
	function GetRandDouble()
	{
		return mt_rand(0, 1000000000)/1000000000;
	}

	function GetRandomStringFromArray($count,$index,$values)
	{	
		$dRandNumber=$this->GetRandDouble();
		$dSumNumber=$index[0];	
		
		for($i=0;$i<$count-1;$i++)
		{
			if($dRandNumber<$dSumNumber)
			{
				return $values[$i];
			}
			$dSumNumber+=$index[$i+1];
		}
		
		return $values[$count-1];
	}
	
	function mysql_update($table, $update, $where)
	{
		$fields = array_keys($update);
		$values = array_values($update);
		$max=count($update);
		 $i=0;
		 $query="UPDATE ".$table." SET ";
		 while($i<$max){
		   if($i!=0){$query.=", ";}
		 $query.=$fields[$i]." = '".$values[$i]."'";
		 $i++;
		 }
		 $query.=" WHERE ".$where." LIMIT 1;";
				 		 
		 mysql_query($query) or die("mysql_update:\n".mysql_error()."\n".$query."\n");
		 	 
		 return true;
		
	}
	function mysql_insert($table, $toAdd)
	{

	   $fields = implode(array_keys($toAdd), ',');
		$values = "'".implode(array_values($toAdd), "','")."'";

	   $q = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES ('.$values.')';
	
	   return mysql_query($q);  
	}

	function mysql_insert_ex($table, $inserts,$func,$ext_inserts)
	{
		$values = array_map('mysql_real_escape_string', array_values($inserts));
		$keys = array_keys($inserts);
		$ext_values = array_map('mysql_real_escape_string', array_values($ext_inserts));
		$ext_keys = array_keys($ext_inserts);
	
		return mysql_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`,`'.implode('`,`', $ext_keys).'`) VALUES (\''.implode('\',\'', $values).'\','.$func.'(\''.implode('\'),'.$func.'(\'', $ext_values).'\'))');
	}
	

	function GetDaysBetweenDates($startDate,$endDate)
	{
		return round((strtotime($endDate) - strtotime($startDate)) / (60 * 60 * 24));
	}

		
	private function GetCityInfo()
	{
		$aCity=array();		
		$qShowStatus 		= "SELECT * FROM `ui__miasta` order by rand() limit 1";
		$qShowStatusResult 	= mysql_query($qShowStatus);			
		$row = mysql_fetch_assoc($qShowStatusResult);		
		$iPos=strpos($row["miej"]," (");
		$aCity["name"]=$row["miej"];
		$aCity["code_post"]="";
		
		$aCity["code_post"]=$row["kod"];
		
		if($iPos !== false)
		{
			$aCity["name"]=substr($row["miej"],0,$iPos);
		}
		
		$aCity["terytory"]=array_search($this->CutPolnishChar(strtolower ( $row["woj"])),$this->Terytory);// $this->Terytory[strtolower($row["woj"])];
		return $aCity;
	}
	
	public function GetVorname($bSex)
	{	
		$qShowStatus 		= "SELECT * FROM `ui__imiona` WHERE plec='".(($bSex)?"M":"K")."' order by rand() limit 1";
		$qShowStatusResult 	= mysql_query($qShowStatus);			
		$row = mysql_fetch_assoc($qShowStatusResult);		
		return $row["imie"];
	}
	
	public function GetName($bSex)
	{
		$qShowStatus 		= "SELECT * FROM `ui__nazwiska` where plec='".(($bSex)?"M":"K")."' order by rand() limit 1";
		$qShowStatusResult 	= mysql_query($qShowStatus);			
		$row = mysql_fetch_assoc($qShowStatusResult);		
		return $row["nazwisko"];
	}	
	
	public function GetSex()
	{
		return mt_rand(0,1);
	}
	
	
	public function MixMail_ReverceVorname($sName,$sVorname)
	{
		while($this->iDot==($func=$this->GetRandomStringFromArray(3,array(0.33,0.33,0.34),array('','.','_'))));	
		$this->iDot=$func;
		return $sName.$func.$sVorname;
	}
	
	public function MixMail_OnlyName($sName,$sVorname)
	{
		return $sName.$this->GenereteID(mt_rand(1,4));
	}
	
	public function MixMail_OnlyVorName($sName,$sVorname)
	{
		return $sVorname.$this->GenereteID(mt_rand(1,4));
	}
	
	public function MixMail_SimpleVorname($sName,$sVorname)
	{
		$iDot=1;//mt_rand(1,strlen($sVorname));	
		while($this->iDot==($func=$this->GetRandomStringFromArray(3,array(0.33,0.33,0.34),array('','.','_'))));	
		$this->iDot=$func;
		return substr($sVorname,0,$iDot).$func.$sName;
	}
	
	public function MixMail_FullName($sName,$sVorname)
	{
		while($this->iDot==($func=$this->GetRandomStringFromArray(3,array(0.33,0.33,0.34),array('','.','_'))));	
		$this->iDot=$func;
		return $sVorname.$func.$sName;
	}
	
	public function MixMail_AddEnd($sName,$birthYear)
	{
		$func=$this->GetRandomStringFromArray(5,array(0.80,0.05,0.05,0.05,0.05),array('','aa',mt_rand(0,9),$birthYear,''));
		return $sName.$func;
	}
	
	public function IsEmailExists($email)
	{
		$qShowStatus 		= "SELECT * FROM `sf_guard_user` where email='".$email."'";
		
		$qShowStatusResult 	= mysql_query($qShowStatus);
		if($qShowStatusResult)
		{			
			return false;
		}
		
		echo $qShowStatus."\n";
		
		return true;
	}	
	
	public function GenerateEmail($sName,$sVorname,$birthYeer='1987')
	{
		$smpt=$this->GetRandomStringFromArray(19,array(0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684,
		0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684,
		0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684,0.0526315789473684),	
		array('wp.pl', 'o2.pl', 'go2.pl', 'tlen.pl', 'gmail.com', 'yahoo.com', 'hotmail.com', 'onet.pl', 'op.pl',
		'poczta.onet.pl', 'onet.eu', 'vp.pl', 'autograf.pl', 'spoko.pl', 'opoczta.pl', 'onet.com.pl', 'interia.pl', 'interia.eu', 'poczta.fm'));
		
		$func=$this->GetRandomStringFromArray(5,array(0.3,0.05,0.05,0.3,0.3),array('MixMail_ReverceVorname','MixMail_OnlyName','MixMail_OnlyVorName','MixMail_SimpleVorname','MixMail_FullName'));
		$sMailLogin=call_user_func(array('UserGenerator', $func),strtolower($sName),strtolower($sVorname));
		
		$sMailLogin=$this->MixMail_AddEnd($sMailLogin,$birthYeer);
		
		$iCounter=0;
		$sTmpName=$sMailLogin."@".$smpt;		
		
		while($this->IsEmailExists($sTmpName))
		{
			$sTmpName=$sMailLogin.$iCounter."@".$smpt;
			$iCounter++;
		}
		
		return array($sMailLogin,$sMailLogin."@".$smpt);
	}
		
	function CutPolnishChar($tekst)
	{		
		$tabela = Array(
		   //WIN
			"\xb9" => "a", "\xa5" => "A", "\xe6" => "c", "\xc6" => "C",
		"\xea" => "e", "\xca" => "E", "\xb3" => "l", "\xa3" => "L",
		"\xf3" => "o", "\xd3" => "O", "\x9c" => "s", "\x8c" => "S",
		"\x9f" => "z", "\xaf" => "Z", "\xbf" => "z", "\xac" => "Z",
		"\xf1" => "n", "\xd1" => "N",
	   //UTF
		"\xc4\x85" => "a", "\xc4\x84" => "A", "\xc4\x87" => "c", "\xc4\x86" => "C",
		"\xc4\x99" => "e", "\xc4\x98" => "E", "\xc5\x82" => "l", "\xc5\x81" => "L",
		"\xc3\xb3" => "o", "\xc3\x93" => "O", "\xc5\x9b" => "s", "\xc5\x9a" => "S",
		"\xc5\xbc" => "z", "\xc5\xbb" => "Z", "\xc5\xba" => "z", "\xc5\xb9" => "Z",
		"\xc5\x84" => "n", "\xc5\x83" => "N",
	   //ISO
		"\xb1" => "a", "\xa1" => "A", "\xe6" => "c", "\xc6" => "C",
		"\xea" => "e", "\xca" => "E", "\xb3" => "l", "\xa3" => "L",
		"\xf3" => "o", "\xd3" => "O", "\xb6" => "s", "\xa6" => "S",
		"\xbc" => "z", "\xac" => "Z", "\xbf" => "z", "\xaf" => "Z",
		"\xf1" => "n", "\xd1" => "N");

	   return strtr($tekst,$tabela);	
	}
   
	
		
	public function GenereteID($iCount)
	{
		$sID='';
		
		for($i=0;$i<$iCount;$i++)
		{
			$sID=chr(mt_rand(97,102)).$sID;
		}	
		
		return $sID;
	}	
	
	function GetIndexValue($tablename)
	{
		$qShowStatus 		= "SHOW TABLE STATUS LIKE '$tablename'";
		$qShowStatusResult 	= mysql_query($qShowStatus);
		$row = mysql_fetch_assoc($qShowStatusResult);
		return $row['Auto_increment'];	
	}
	
	private function GenerateBirthDate()
	{
		$dt=new DateTime("01/01/1972");
		$dt->add(new DateInterval('P'.rand(0,$this->iDays).'D'));
		return $dt;
	}
	
	public function Generate($iCount)
	{			
		$cre_date=new DateTime('01/01/2011');		
		
		$usrid=$this->GetIndexValue('sf_guard_user');
		
		$usercs=array(500,900,1500,2200,3000 ,3600,4200 ,4900,5000,5100);
		
		foreach($usercs as $userc)
		{
			for($i=0;$i<=$userc;$i++)
			{			
				$reg_dt=clone $cre_date;
				$reg_dt->add(new DateInterval('PT'.rand(8,22).'H'));
				$reg_dt->add(new DateInterval('PT'.rand(0,59).'M'));
				$reg_dt->add(new DateInterval('PT'.rand(0,59).'S'));
				$this->SingleGenerate($reg_dt,$usrid+$i);
			}
		}
	}
	
	public function SingleGenerate($dt,$usrid)
	{		
		$bSex=$this->GetSex();
		
		$sOrginalName=$this->GetName($bSex);
		$sOrginalVorname=$this->GetVorname($bSex);
		
		$sName=$this->CutPolnishChar($sOrginalName);
		$sVorname=$this->CutPolnishChar($sOrginalVorname);		
		
		$dtBirthDate=$this->GenerateBirthDate();
		$sEmail=$this->GenerateEmail($sName,$sVorname,$dtBirthDate->format('Y'));
		
		$aCity=$this->GetCityInfo();				
		
		$login_dt=clone $dt;
		$login_dt->add(new DateInterval('P'.rand(0,$this->GetDaysBetweenDates($dt->format('m/d/Y'),"08/20/2011")).'D'));
		$login_dt->add(new DateInterval('PT'.rand(8,22).'H'));
		$login_dt->add(new DateInterval('PT'.rand(0,59).'M'));
		$login_dt->add(new DateInterval('PT'.rand(0,59).'S'));		
		
		$params=array(
		'username'=>$sEmail[0],
		'email'=>$sEmail[1],
		'algorithm'=>'sha1',
		'salt'=>$this->GenereteID(12),				
		'password'=>sha1($this->GenereteID(12)),
		'created_at'=>'',
		'last_login'=>$login_dt->format('Y-m-d H:i:s'),
		'is_active'=>'1',
		'is_super_admin'=>'0'
		);
		$query=$this->mysql_insert('sf_guard_user',$params);
		if(!$query)
		{						
			echo mysql_error().'\n';			
		}
		
		$params=array(
		'user_id'=>$usrid,
		'first_name'=>$sOrginalVorname,
		'last_name'=>$sOrginalName,
		'street'=>'',		
		'city'=>$aCity["name"],
		'post_code'=>$aCity["code_post"],
		'updated_at'=>'');
		$query=$this->mysql_insert('sf_guard_user_profile',$params);
		if(!$query)
		{						
			echo mysql_error().'\n';			
		}
		
		

		
		
	}	
	
}	
	
?>